package leetcode.primary;

public class T10 {
    int len_s,len_p,tail_s,tail_p;
    public boolean isMatch(String s, String p) {
        len_s = s.length() ;
        len_p = p.length() ;
        tail_s = len_s-1;
        tail_p = len_p-1;
        while(tail_s!=0 && tail_p!=0){
            switch (tail_p){
                case '.':
                    pointsolve();
                    break;
                case '*':
                    starsolve(s,p);
                    break;
                default :
                    if(normalsolve(s,p)){
                            return true;
                    }else{
                        return false;
                    }
            }
        }
        return true;
    }
    public void pointsolve(){
        tail_p--;
        tail_s--;
    }
    public void starsolve(String s,String p){
        int flag = 0;
        char tempt = p.charAt(tail_p-1);
        if(tempt=='.'){
            tempt = s.charAt(tail_s);
        }
        while(s.charAt(tail_s)==tempt){
            tail_s--;
            flag=1;
        }
        if(flag==0){
            tail_p--;
        }else{
            tail_p-=2;
        }
    }
    public boolean normalsolve(String s,String p){
        if(s.charAt(tail_s)!=p.charAt(tail_p)){
            return false;
        }else{
            return true;
        }
    }
}
